programming4us
           
 
 
Windows

Windows Azure Storage : REST API (part 2) - Storage Client APIs

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
11/21/2010 11:17:52 AM

3. Storage Client APIs

Even though the REST API and the operations in the REST API are easily readable, the API doesn't automatically create client stubs like the ones created by WDSL-based web services. You have to create your own client API and stubs for REST API operations. This makes the client programming more complex and increases the barrier to entry for developers. To reduce this barrier to entry, the Windows Azure SDK team has created two client helper libraries: Microsoft.WindowsAzure.StorageClient from Windows Azure SDK, and the Storage Client code sample. Both libraries are used to invoke REST APIs of the Windows Azure Storage service. The Microsoft.WindowsAzure.StorageClient library abstracts this by providing a closed-source interface and therefore is less interesting to developers who want to see the inner workings of the method calls.

In the Windows Azure CTP version, a code sample named Storage Client became the choice of developers for calling storage services because the Microsoft.WindowsAzure.StorageClient wasn't available and also because it was open source. It provided good insight into building your own storage client library. The Storage Client made it easier to see the end-to-end method calls to the Storage service. This chapter uses the Storage Client code sample in sample applications and covers the class structure and calling mechanisms in the Microsoft.WindowsAzure.StorageClient namespace.

The following sections cover the queue storage APIs from Microsoft.WindowsAzure.StorageClient and the Storage Client code sample.

NOTE

You don't have to use the Storage Client to make REST calls to the Storage service. You can create your own client library for making REST operations to the Storage service. In order to keep the book conceptual, I use the Storage Client code sample helper library to interact with the Storage service throughout this book. The source code for Storage Client is available in the samples directory of the Windows Azure SDK or from the Windows Azure code samples site, http://code.msdn.microsoft.com/windowsazuresamples.

3.1. Windows Azure Storage Client Queue API

The Microsoft.WindowsAzure.StorageClient namespace consists of classes representing the entire queue hierarchy. Figure 1 illustrates the core classes for programming Queue service applications.

Figure 1. Queue class hierarchy

As shown in Figure 1, four core classes are required for queue operations. Table 1 lists these classes and a short description of each of them.

The Windows Azure Storage Client API is the recommended method for programming Storage service applications. The API provides synchronous as well as asynchronous methods for interacting with the Storage service REST API.


Table 1. Classes for the Queue Service
Class NameDescription
CloudStorageAccountA helper class for retrieving account information from the configuration file or creating an instance of the storage account object from account parameters.
CloudQueueClientA wrapper class for getting references to the core queue objects. The class consists of methods like GetQueueReference() and ListQueues().
CloudQueueConsists of queue operations like Create(), Delete(), AddMessage(), and GetMessage().
CloudQueueMessageRepresents a queue message with properties like InsertionTime, ExpirationTime, NextVisibleTime, Id, and PopReceipt.

In addition to these core classes, classes like QueueAttributes and QueueErrorCodeStrings represent more details about the queue.

The steps for programming simple queue applications with the queue classes listed in Table 1 are as follows:

  1. Add the following using statement to your C# class:

    using Microsoft.WindowsAzure.StorageClient;

  2. Instantiate the CloudStorageAccount class from the configuration file:

    CloudStorageAccount  storageAccountInfo =
    CloudStorageAccount.FromConfigurationSetting(configurationSettingName);

    Or, instantiate the CloudStorageAccount class using account information:

    CloudStorageAccount  storageAccountInfo = new CloudStorageAccount(new
    StorageCredentialsAccountAndKey(accountName, accountKey), new Uri
    (blobEndpointURI), new
    Uri(queueEndpointURI), new Uri(tableEndpointURI));

  3. Create an instance of CloudQueueClient:

    CloudQueueClient queueStorageType = storageAccountInfo. CreateCloudQueueClient ();


When you have an instance of the CloudQueueClient class, you can execute operations on the queue storage service as follows:

List queues:

IEnumerable<CloudQueue> queues = queueStorageType.ListQueues();
Create Queue
queueStorageType.GetQueueReference(queueName).Create();
Delete Queue
queueStorageType.GetQueueReference(queueName).Delete();

Add a message:

public void AddMessage(string queueName, CloudQueueMessage queueMessage)
{
queueStorageType.GetQueueReference(queueName).AddMessage(queueMessage);
}

Get messages:

queueStorageType.GetQueueReference(queueName).GetMessages
(numberofMessages, TimeSpan.FromSeconds(visibilityTimeoutInSecs));

Peek messages:

queueStorageType.GetQueueReference(queueName).PeekMessages(numberofMessages);


Delete a message:

public void DeleteMessage(string queueName, CloudQueueMessage queueMessage)
{
queueStorageType.GetQueueReference(queueName).DeleteMessage(queueMessage);
}

Set queue metadata:

public void SetQueueMetadata(string queueName, NameValueCollection queueProps)
{

CloudQueue queue = queueStorageType.GetQueueReference(queueName);
queue.Attributes.Metadata = queueProps;
queue.SetMetadata();
}


The call to SetMetadata() method calls the method on the queue service API in the cloud.

3.2. Storage Client Code Sample Queue API

The Storage Client project consists of five important classes in the context of the Queue service. Table 2 lists these classes and a short description of each of them.

Table 2. Storage Client classes for the Queue Service
Class NameDescription
StorageAccountInfoA helper class for retrieving account information from the configuration file.
QueueStorageAn abstract class that has properties and methods at the account level of the Queue service hierarchy. It has an AccountName property and the method ListQueues().
QueueStorageRestA class that extends the QueueStorage class and implements the abstract methods. This class also has a nested class ListQueueResult, representing the results returned by the Queue service.
MessageQueueAn abstract class with signatures for the queue and message operations. Some example method calls are CreateQueue(), PutMessage(), and GetMessages().
QueueRestA class that extends the MessageQueue class and implements the abstract methods.

Figure 2 illustrates the five classes listed in Table 2 and the relationships between them.

Figure 2. StorageClient classes for the Queue service

Following are some of the typical steps required to interact with the Queue service using Storage Client classes.

  1. Add your account information in the <appSettings> section of your application configuration file (app.config or web.config):

    <add key = "AccountName" value="[Your Account Name]"/>
    <add key = "AccountSharedKey"
    value="[Your Account Shared Key from the Developer Portal]"/>

    To work with local storage, add "devstoreaccount1" as the AccountName and "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==" as the shared key.

  2. Add the Queue service endpoint URI in the appSettings section of your application configuration file:

    <add key="QueueStorageEndpoint" value="http://blob.core.windows.net"/>

    To work with local storage, add the URI of the local Queue service resource, http://127.0.0.1:10001.

  3. Create an instance of the StorageAccountInfo class from the configuration information you entered in the configuration file:

    StorageAccountInfo account =
    StorageAccountInfo.GetDefaultQueueStorageAccountFromConfiguration();

  4. Create an instance of the QueueStorage class based on the account object:

    QueueStorage queueStorage = QueueStorage.Create(account);

  5. Call the ListQueues() method on the queueStorage object to get a list of all the queues in the account:

    queueStorage.ListQueues();

  6. To access a particular queue, call

    MessageQueue messageQueue = queueStorage.GetQueue(queueName);

    When you have a reference to the message queue object, then you can call not only queue methods like CreateQueue(), DeleteQueue(), and ListQueues(), but also message functions like PutMessage(), GetMessages(), and DeleteMessage().

Other -----------------
- Windows 7 : Customizing Your Notebook’s Power and Sleep Buttons
- Windows 7 : Customizing the Start Menu’s Power Button
- Windows 7 : Turning Off Your Windows 7 Computer from Anywhere
- Windows 7 : Setting Up One-Click Restarts and Shutdowns
- Windows 7 : Useful Windows 7 Logon Strategies
- Windows 7 : Customizing Startups with the Advanced Options Menu
- Windows 7 : Customizing Startups Using the Boot Configuration Data
- Windows Azure : Queue Service Architecture
- Windows 7 : Customizing Windows 7’s Open With List
- Windows 7 : Customizing the New Menu
- Windows 7 : Creating a New File Type
- Windows Vista - Sharing Files and Folders : Accessing a Shared Folder
- Windows Vista - Sharing Files and Folders : Standard Sharing
- Windows Vista - Sharing Files and Folders : Public Folder
- Windows Vista - Sharing Files and Folders : Network Discovery and Browsing
- Windows 7 : Manage Your Network - Working with a Homegroup
- Windows 7 : Manage Your Network - Creating a Homegroup
- Windows 7 : Manage Your Network - Connecting to a Network
- Multibooting Windows 7
- Windows 7 Customization : Working with Existing File Types
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us